package cn.com.infcn.spider.io.input.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.nlpcn.commons.lang.util.MD5;
import org.nutz.dao.entity.Record;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;

import cn.com.infcn.spider.URIManager;
import cn.com.infcn.spider.io.input.DBIteartorInput;
import cn.com.infcn.spider.util.BasicDao;
import cn.com.infcn.spider.util.MongoDao;
import cn.com.infcn.spider.util.StaticValue;

public class jdbcInput implements DBIteartorInput {

	private BasicDao basicDao = null;

	private String sql = null;

	private Object flag = null;
	
	private Object type = null;		//1:分页查询  2：增量查询

	private String flagName = null;

	private sortMethod comp = null;

	private String id = null;

	private String lastCode = null;
	
	private String jobName ;

	public jdbcInput(JSONObject job, String jobName)  {

		basicDao = new BasicDao(job.getString("jdbcUrl"), job.getString("username"), job.getString("password"));

		sql = job.getString("sql");

		id = MD5.code(job.getString("jdbcUrl") + sql);
		
		type = job.getString("type");
		
		this.jobName = jobName ;

		Object sortValue = StaticValue.getBean(MongoDao.class, "mongoDao").findById(URIManager.SUCCESS + jobName, id);

		if (sortValue != null) {
			BasicDBObject flag = (BasicDBObject)sortValue;
			this.flag = flag.get("flag");
		} else {
			this.flag = job.get("flag");
		}

		if ("2".equals(this.type) &&this.flag != null) { // 寻找大于小于标识
			String[] split = sql.split("\\s+");

			int index = 0;

			for (int i = 0; i < split.length; i++) {
				if ("%s".equals(split[i])) {
					index = i;
					break;
				}
			}

			if (index >= 2) {

				switch (split[index - 1]) {
				case ">":
				case ">=":
					comp = sortMethod.GT;
					break;
				case "<":
				case "<=":
					comp = sortMethod.LT;
					break;
				}

				flagName = split[index - 2];
			}

		}
	}

	@Override
	public List<?> all() {
		if (this.flag != null)
			return basicDao.select(String.format(sql, flag));
		else
			return basicDao.select(sql);
	}

	public List<?> iterator() {
		List<Record> select = null;
		if (flag != null) {
			select = basicDao.select(String.format(sql, flag));
		} else {
			select = basicDao.select(sql);
		}
		if(select != null && select.size() > 0 ){
			Record last = select.get(select.size() - 1);
	
			String tempCode = last.toString();
	
			if (tempCode.equals(lastCode)) {
				return Collections.emptyList();
			}
			lastCode = tempCode;
			changeFlag(flag, last, select.size());
	
			return select;
		}else{
			return Collections.emptyList();
		}
	}

	@SuppressWarnings("all")
	public void changeFlag(Object flag, Map<String, Object> obj, int recordSize) {
		//如果是分页，则把当前页码+1；
		if("1".equals(this.type)){
			this.flag = (Integer)flag+recordSize;
		}
		//增量
		else if("2".equals(this.type)){
			if (comp == null || flagName == null) {
				return;
			}
			
			Object newFlag = obj.get(flagName);
			
			Object _flag = null;
			Object _newFlag = null;
			
			//判断两个类型是否一致,如果不一致，按照字符串进行比较
			if(newFlag.getClass() != flag.getClass()){
				_flag = flag.toString();
				_newFlag = newFlag.toString();
			}else{
				_flag = flag;
				_newFlag = newFlag;
			}
			
			if (sortMethod.GT.equals(comp) || sortMethod.GTE.equals(comp)) {
				if (((Comparable) _flag).compareTo(_newFlag) < 0) {
					this.flag = newFlag;
				}
			} else {
				if (((Comparable) _flag).compareTo(_newFlag) > 0) {
					this.flag = newFlag;
				}
			}
			
		}
		BasicDBObject dbo = new BasicDBObject("_id", id).append("flag", this.flag);

		StaticValue.getBean(MongoDao.class, "mongoDao").save(URIManager.SUCCESS + jobName, dbo);

	}

	public sortMethod getComp() {
		return comp;
	}

	public String getFlagName() {
		return flagName;
	}

	public String getId() {
		return id;
	}

	public Object getFlag() {
		return flag;
	}

	public void setFlag(Object flag) {
		this.flag = flag;
	}

}
